<?php

namespace app\model;

//use support\Model;
use app\controller\IndexController;
use app\controller\wmapi\PublicCommentController;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaSalesOrdersExamineApprove
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_sales_order_examine_approve';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all($condition)
    {
        $pageNum = 10;
        $condition [] = ['is_show', '=', 1];
        $list = Db::table('wa_sales_order_examine_approve')
            ->select('*')
            ->where($condition)
            ->get();

        return json_encode($list);
    }


    public static function postsCount($start_time, $end_time)
    {

        $start_time = date('Y-m-d H:i:s', $start_time);
        $end_time = date('Y-m-d H:i:s', $end_time);
        $provider_count = Db::table('wa_sales_order_examine_approve')
            ->select('*')
            ->where('post_type', 'product')
            ->whereBetween('created_at', [$start_time, $end_time])
            ->count(); // 用户数量
        return $provider_count;

    }


    public static function list($param, $adminId, $page, $pageNum, $whereNotIn, $purchaseOrdersn, $fields = ['*'], $condition = [], $between = [])
    {
        $offset = ($page - 1) * $pageNum;
        // $purchaseOrdersn 为空 就是没有当前登录用户的审核订单
        // 数组转对象  get_object_vars($purchaseOrdersn)
        if (gettype($purchaseOrdersn) == "object") {
            $purchaseOrdersn = get_object_vars($purchaseOrdersn); // 对象转数组函数
        } elseif (gettype($purchaseOrdersn) == "array") {
            $purchaseOrdersn = $purchaseOrdersn;
        }
        $condition [] = ['is_show', '=', 1];

        /**
         * 先获取审批中 待审批的 订单编号 和对应的审批id或编号
         * 审批流中的 部门id 必须是 当前人员的部门
         * 遍历审批流中的 部门admin_ids 中必须有当前人员 有他 但是并没有审批通过的状态就需要展示（因为轮到他时会有一条 0 待审批的状态 其它人会有 通过的状态 或者没有待审批记录）
         *
         */
        $wai_approve_sn = []; // 待当前人员审批的订单
        $where_approve = [];
        $where_approve [] = ['status', '=', 0]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
        $approve = WaSalesOrdersExamineApprove::all($where_approve);
        $approve = json_decode($approve);
        if ($approve) {
            foreach ($approve as $kap) {
//                $where_approve_flow = [];
//                $where_approve_flow [] = ['sale_no', '=', $kap->sale_no];
//                //先判断 是否有 通过的  撤回  驳回的 不用考虑 因为上一步筛选的就是待审批中的审批订单
//                $where_approve_flow [] = ['status', '=', 1]; // 因为轮到他时会有一条 0 待审批的状态 其它人会有 通过的状态 或者没有待审批记录
//                $approve_flow = WaSalesOrdersExamineApproveFlow::all($where_approve_flow);
//                $approve_flow = json_decode($approve_flow);
//                if (!$approve_flow){
                // 因为轮到他时会有一条 0 待审批的状态 其它人会有 通过的状态 或者没有待审批记录  只要排除 有人审核通过 且本人没有审核过 就是本人待审核
                $where_approve_flow_0 = [];
                $where_approve_flow_0 [] = ['sale_no', '=', $kap->sale_no];
                $where_approve_flow_0 [] = ['status', '=', 0]; // 因为轮到他时会有一条 0 待审批的状态 其它人会有 通过的状态 或者没有待审批记录
                $approve_flow_0 = WaSalesOrdersExamineApproveFlow::all($where_approve_flow_0);
                $approve_flow_0 = json_decode($approve_flow_0);
                if ($approve_flow_0) {
                    foreach ($approve_flow_0 as $kapf0) {
                        // 筛出来 还要加一层保险 确保 部门中审核人员 由本人
                        if ($kapf0->admin_ids) {
                            $admin_ids = json_decode($kapf0->admin_ids);
                            if (in_array($adminId->id, $admin_ids)) {
                                // 这个时候把审批订单编号扔出来 就是待当前人员待审核的数据
                                array_push($wai_approve_sn, $kap->sale_no);
                            }
                        }
                    }
                }
//                }
            }

        }
        if ($param['status'] == 4) {
            if ($between) {
                $model = Db::table('wa_sales_order_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereIn('sale_no', $wai_approve_sn)
//                    ->whereNotIn('status',$whereNotIn)
                    ->whereBetween('created_at', $between);
            } else {
                $model = Db::table('wa_sales_order_examine_approve')
                    ->select($fields)
                    ->where($condition)
//                    ->whereNotIn('status',$whereNotIn)
                    ->whereIn('sale_no', $wai_approve_sn);
            }
        } else {
            if ($between) {
                $model = Db::table('wa_sales_order_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereNotIn('status', $whereNotIn)
                    ->whereBetween('created_at', $between);
            } else {
                $model = Db::table('wa_sales_order_examine_approve')
                    ->select($fields)
                    ->where($condition)
                    ->whereNotIn('status', $whereNotIn);
            }
        }


        $count = $model->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();


        if ($list) {
            foreach ($list as $k) {
                //`status` int(11) DEFAULT NULL COMMENT '销售单审批状态 0 审批中 1 通过 2 驳回 3 撤回\r\n',
                $approve = WaSalesOrdersExamineApproveFlow::all(
                    ['sales_order_sn' => $k->sales_order_sn, 'sale_no' => $k->sale_no]);
                $approve = json_decode($approve);
                if ($approve) {
                    foreach ($approve as $kpr) {
                        if ($kpr->admin_ids) {
                            if ($kpr->admin_ids){
                                $kpr->admin_ids = json_decode($kpr->admin_ids);
                            }
                            if ($kpr->status == 3) {
                                $approve_status = 3; // 已驳回
                                $approve_status_name = '已撤回';
                            } elseif ($kpr->status == 2) {
                                $approve_status = 2; // 已驳回
                                $approve_status_name = '已驳回';
                            } elseif ($kpr->status == 1) {
                                if (in_array($adminId->id, $kpr->admin_ids)) {
                                    $approve_status = 1; // 已审批
                                    $approve_status_name = '已审批';
                                } else {
                                    $approve_status = 998; // 审批中
                                    $approve_status_name = '审批中';
                                }

                            } elseif ($kpr->status == 0) {
                                if (in_array($adminId->id, $kpr->admin_ids)) {
                                    $approve_status = 0; // 待审批
                                    $approve_status_name = '待审批';
                                } else {
                                    $approve_status = 998; // 审批中
                                    $approve_status_name = '审批中';
                                }

                            } else {
                                $approve_status = 999; // 已驳回
                                $approve_status_name = '不在审批流中';
                            }
                        } else {
                            $approve_status = 10000; // 已驳回
                            $approve_status_name = '不在审批流中';
                        }


                    }
                } else {
                    $approve_status = 10001; // 审批状态
                    $approve_status_name = '没有审批记录';// 审批状态名称
                }

//                if ($approve) {
//                    foreach ($approve as $kpr) {
//                        $kpr->admin_ids = json_decode($kpr->admin_ids);
//                        if ($kpr->status == 3) {
//                            $approve_status = 3; // 已驳回
//                            $approve_status_name = '已撤回';
//                        } elseif ($kpr->status == 2) {
//                            $approve_status = 2; // 已驳回
//                            $approve_status_name = '已驳回';
//                        } elseif ($kpr->status == 1) {
//                            if (in_array($adminId->id, $kpr->admin_ids)){
//                                $approve_status = 1; // 已审批
//                                $approve_status_name = '已审批';
//                            }else{
//                                $approve_status = 998; // 审批中
//                                $approve_status_name = '审批中';
//                            }
//
//                        } elseif ($kpr->status == 0) {
//                            if (in_array($adminId->id, $kpr->admin_ids)){
//                                $approve_status = 0; // 待审批
//                                $approve_status_name = '待审批';
//                            }else{
//                                $approve_status = 998; // 审批中
//                                $approve_status_name = '审批中';
//                            }
//
//                        } else {
//                            $approve_status = 999; // 已驳回
//                            $approve_status_name = '不在审批流中';
//                        }
//                    }
//                } else {
//                    $approve_status = 10001; // 审批状态
//                    $approve_status_name = '没有审批记录';// 审批状态名称
//                }

                $k->approve_status = $approve_status; // 审批状态
                $k->approve_status_name = $approve_status_name;// 审批状态名称

                $k->approve_flow = json_decode($k->approve_flow);
                // 销售单审批状态 0 审批中 1 通过 2 驳回 3作废 4删除
                if ($k->status == 0) {
                    $k->status_name = '审批中';
                } elseif ($k->status == 1) {
                    $k->status_name = '审批通过';
                } elseif ($k->status == 2) {
                    $k->status_name = '审批驳回';
                } elseif ($k->status == 3) {
                    $k->status_name = '撤回审批';
                } elseif ($k->status == 4) {
                    $k->status_name = '已删除';
                } elseif ($k->status == 5) {
                    $k->status_name = '已撤回';
                } else {
                    $k->status_name = '';
                }
                $orders = WaOrders::find(['order_sn' => $k->sales_order_sn]);
                $orders = json_decode($orders);
                if ($orders) {
                    $k->user_name = $orders->user_name;
                    $k->cnh = $orders->cnh;
                    $k->currency = $orders->currency;


                    $k->currency_name = \app\controller\wmapi\IndexController::currencyNameCommon($orders->currency);

                } else {

                    $k->user_name = '';
                    $k->cnh = 0;
                    $k->currency_name = '';
                    $k->currency = '';

                }
            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }

    /**
     * 销售订单审批-查询
     * @param $data
     * @return int
     */
    public static function find($condition, $adminId)
    {
        $condition [] = ['is_show', '=', 1];
        $res = Db::table('wa_sales_order_examine_approve')
            ->where($condition)
            ->first();
        if ($res) {
            // 获取订单的 对应的审批编号
            $orders = WaOrders::find(['order_sn' => $res->sales_order_sn]);
            $orders = json_decode($orders);

            if ($orders) {
                if ($orders->sale_no) {
                    $approve = WaSalesOrdersExamineApproveFlow::all(
                        ['sales_order_sn' => $res->sales_order_sn, 'sale_no' => $res->sale_no]);
                    $approve = json_decode($approve);
                    if ($approve) {
                        foreach ($approve as $kpr) {
                            if ($kpr->admin_ids){
                                $kpr->admin_ids = json_decode($kpr->admin_ids);
                            }
                            if ($kpr->status == 3) {
                                $approve_status = 3; // 已驳回
                                $approve_status_name = '已撤回';
                            } elseif ($kpr->status == 2) {
                                $approve_status = 2; // 已驳回
                                $approve_status_name = '已驳回';
                            } elseif ($kpr->status == 1) {
                                if (in_array($adminId->id, $kpr->admin_ids)) {
                                    $approve_status = 1; // 已审批
                                    $approve_status_name = '已审批';
                                } else {
                                    $approve_status = 998; // 审批中
                                    $approve_status_name = '审批中';
                                }

                            } elseif ($kpr->status == 0) {
                                if (in_array($adminId->id, $kpr->admin_ids)) {
                                    $approve_status = 0; // 待审批
                                    $approve_status_name = '待审批';
                                } else {
                                    $approve_status = 998; // 审批中
                                    $approve_status_name = '审批中';
                                }

                            } else {
                                $approve_status = 999; // 已驳回
                                $approve_status_name = '不在审批流中';
                            }
                        }
                    } else {
                        $approve_status = 10001; // 审批状态
                        $approve_status_name = '没有审批记录';// 审批状态名称
                    }

                    $res->approve_status = $approve_status; // 审批状态
                    $res->approve_status_name = $approve_status_name;// 审批状态名称
                }
            } else {

                $approve_status = 10001; // 审批状态
                $approve_status_name = '没有审批记录';// 审批状态名称

                $res->approve_status = $approve_status; // 审批状态
                $res->approve_status_name = $approve_status_name;// 审批状态名称

            }

        }

        return json_encode($res);
    }

    /**
     * 轮到我审批的订单
     * 先判断 是否有 通过的  撤回  驳回的 不用考虑 因为上一步筛选的就是待审批中的审批订单
     * 但是待审批中的审批订单也存在审批未完成 就被删除 作废 撤回的情况
     * 最后的解决方法是：
     * 筛选所有本人的的待审批订单  即 状态为 0 的   然后根据对应的订单编号 去查询对应的订单 当前的状态是什么 必须是待审批状态才展示
     * @param $adminId
     * @param $page
     * @param $pageNum
     * @param $purchaseOrdersn
     * @param array $fields
     * @param array $condition
     * @param array $between
     * @return false|string
     */
    public static function waitApproveByAdminIdList($adminId, $page, $pageNum, $purchaseOrdersn, $fields = ['*'], $condition = [], $between = [])
    {
        $offset = ($page - 1) * $pageNum;
        // $purchaseOrdersn 为空 就是没有当前登录用户的审核订单
        // 数组转对象  get_object_vars($purchaseOrdersn)
        if (gettype($purchaseOrdersn) == "object") {
            $purchaseOrdersn = get_object_vars($purchaseOrdersn); // 对象转数组函数
        } elseif (gettype($purchaseOrdersn) == "array") {
            $purchaseOrdersn = $purchaseOrdersn;
        }


        // 筛选所有本人的的待审批订单 先缩小范围 再去审批流查
        $wai_approve_sn = []; // 待当前人员审批的订单
        $where_approve = [];
        $where_approve [] = ['status', '=', 0]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
        $approve = WaSalesOrdersExamineApprove::all($where_approve);
        $approve = json_decode($approve);
        if ($approve) {
            foreach ($approve as $kap) {
                $where_order = [];
                $where_order [] = ['order_sn', '=', $kap->sales_order_sn];
                $order_data = WaOrders::find($where_order);
                $order_data = json_decode($order_data);
                if ($order_data) {
                    PublicCommentController::销售订单状态;
                    // 主订单是审批中的 才需要展示在待审批列表
                    if ($order_data->status == 2) {
                        var_dump($kap->sale_no);
                        // 现在开始查审批流
                        $where_approve_flow = [];
                        $where_approve_flow [] = ['sale_no', '=', $kap->sale_no]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
                        $where_approve_flow [] = ['status', '=', 0]; // 先获取审批中 待审批的 订单编号 和对应的审批id或编号
                        $approve_flow = WaSalesOrdersExamineApproveFlow::find($where_approve_flow);
                        $approve_flow = json_decode($approve_flow);
                        if ($approve_flow) {
                            if ($approve_flow->admin_ids) {
                                $admin_ids = json_decode($approve_flow->admin_ids);
                                if (in_array($adminId->id, $admin_ids)) {
                                    // 这个时候把审批订单编号扔出来 就是待当前人员待审核的数据
                                    array_push($wai_approve_sn, $kap->sale_no);
                                }
                            }
                        }

                    }
                }
            }
        }


        $model = Db::table('wa_sales_order_examine_approve')
            ->select($fields)
            ->where($condition)
            ->whereIn('sale_no', $wai_approve_sn);
        $count = $model->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();


        if ($list) {
            foreach ($list as $k) {
                $approve = WaSalesOrdersExamineApproveFlow::all(
                    ['sales_order_sn' => $k->sales_order_sn, 'sale_no' => $k->sale_no]);
                $approve = json_decode($approve);
                if ($approve) {
                    foreach ($approve as $kpr) {
                        if ($kpr->admin_ids){
                            $kpr->admin_ids = json_decode($kpr->admin_ids);
                        }
                        if ($kpr->status == 3) {
                            $approve_status = 3; // 已驳回
                            $approve_status_name = '已撤回';
                        } elseif ($kpr->status == 2) {
                            $approve_status = 2; // 已驳回
                            $approve_status_name = '已驳回';
                        } elseif ($kpr->status == 1) {
                            if (in_array($adminId->id, $kpr->admin_ids)) {
                                $approve_status = 1; // 已审批
                                $approve_status_name = '已审批';
                            } else {
                                $approve_status = 1; // 审批中
                                $approve_status_name = '审批中';
                            }

                        } elseif ($kpr->status == 0) {
                            if (in_array($adminId->id, $kpr->admin_ids)) {
                                $approve_status = 0; // 待审批
                                $approve_status_name = '待审批';
                            } else {
                                $approve_status = 998; // 审批中
                                $approve_status_name = '审批中';
                            }

                        } else {
                            $approve_status = 999; // 已驳回
                            $approve_status_name = '不在审批流中';
                        }
                    }
                } else {
                    $approve_status = 10001; // 审批状态
                    $approve_status_name = '没有审批记录';// 审批状态名称
                }

                $k->approve_status = $approve_status; // 审批状态
                $k->approve_status_name = $approve_status_name;// 审批状态名称

                $k->approve_flow = json_decode($k->approve_flow);
                // 销售单审批状态 0 审批中 1 通过 2 驳回 3作废 4删除
                if ($k->status == 0) {
                    $k->status_name = '审批中';
                } elseif ($k->status == 1) {
                    $k->status_name = '审批通过';
                } elseif ($k->status == 2) {
                    $k->status_name = '审批驳回';
                } elseif ($k->status == 3) {
                    $k->status_name = '撤回审批';
                } elseif ($k->status == 4) {
                    $k->status_name = '已删除';
                } elseif ($k->status == 5) {
                    $k->status_name = '已撤回';
                } else {
                    $k->status_name = '';
                }
                $orders = WaOrders::find(['order_sn' => $k->sales_order_sn]);
                $orders = json_decode($orders);
                if ($orders) {
                    $k->user_name = $orders->user_name;
                    $k->cnh = $orders->cnh;
                    $k->currency = $orders->currency;

                    $k->currency_name = \app\controller\wmapi\IndexController::currencyNameCommon($orders->currency);

                } else {

                    $k->user_name = '';
                    $k->cnh = 0;
                    $k->currency_name = '';
                    $k->currency = '';

                }
            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }


    /**
     * 销售订单审批 -创建
     * @param $data
     * @return int
     */
    public static function create($data)
    {
        return Db::table('wa_sales_order_examine_approve')->insertGetId($data);
    }


    /**
     * 销售订单审批-状态修改
     * @param $data
     * @return int
     */
    public static function updated($condition, $data)
    {
        return Db::table('wa_sales_order_examine_approve')->where($condition)->update($data);
    }


}